package com.csun.cmny.provider.controller.admin;

import com.csun.cmny.provider.model.constant.CsunSleepConstant;
import com.csun.cmny.provider.model.domain.DmcCsunSleepFunc;
import com.csun.cmny.provider.model.enums.device.DeviceFileUrlEnum;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.DmcCsunSleepFuncService;
import com.csun.cmny.util.PublicUtil;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.google.common.base.Preconditions;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2020-12-22 09:50
 * @Description:
 */
@Slf4j
@RestController
@RequestMapping(value = "/csunSleep", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class DmcCsunSleepFuncController {

    @Resource
    private DmcCsunSleepFuncService dmcCsunSleepFuncService;
    @Resource
    private CmnyMessageSource cmnyMessageSource;

    /**
     * 查询离床功能码
     */
    @RequestMapping(value = "/getOffBedFunc", method = RequestMethod.POST)
    public Wrapper getOffBedFunc(Long groupId) {

        Preconditions.checkArgument(groupId != null,
                cmnyMessageSource.getMessage("group.id.null", null));

        DmcCsunSleepFunc dmcCsunSleepFunc = new DmcCsunSleepFunc();
        dmcCsunSleepFunc.setGroupId(groupId);
        dmcCsunSleepFunc.setFuncCode(CsunSleepConstant.OFF_BED);
        dmcCsunSleepFunc = dmcCsunSleepFuncService.selectOne(dmcCsunSleepFunc);
        if (PublicUtil.isEmpty(dmcCsunSleepFunc)) {
            dmcCsunSleepFunc = new DmcCsunSleepFunc();
            dmcCsunSleepFunc.setGroupId(groupId);
            dmcCsunSleepFunc.setFuncCode(CsunSleepConstant.OFF_BED);
            dmcCsunSleepFunc.setStat(0);
            dmcCsunSleepFuncService.save(dmcCsunSleepFunc);
        }

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,
                cmnyMessageSource.getMessage("operation.success", null),
                dmcCsunSleepFunc);
    }

    /**
     * 查询离床功能码
     */
    @RequestMapping(value = "/setOffBedFunc", method = RequestMethod.POST)
    public Wrapper setOffBedFunc(Long groupId, Integer stat) {

        Preconditions.checkArgument(stat != null,
                cmnyMessageSource.getMessage("stat.null", null));
        Preconditions.checkArgument(stat >= 0 && stat <= 1,
                cmnyMessageSource.getMessage("stat.range", null));


        DmcCsunSleepFunc dmcCsunSleepFunc = new DmcCsunSleepFunc();
        dmcCsunSleepFunc.setGroupId(groupId);
        dmcCsunSleepFunc.setFuncCode(CsunSleepConstant.OFF_BED);

        dmcCsunSleepFuncService.delete(dmcCsunSleepFunc);

        dmcCsunSleepFunc.setStat(stat);
        dmcCsunSleepFuncService.save(dmcCsunSleepFunc);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,
                cmnyMessageSource.getMessage("operation.success", null),
                dmcCsunSleepFunc);
    }


}
